package com.example.eureka_client.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eureka_client.entity.AttentionQuestion;
import com.example.eureka_client.entity.Question;
import com.example.eureka_client.httpResult.Result;
import com.example.eureka_client.httpResult.ResultGenerator;
import com.example.eureka_client.service.IAttentionQuestionService;
import com.example.eureka_client.service.IQuestionService;
import com.example.eureka_client.utils.PageRequest;
import com.example.eureka_client.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 问题表 前端控制器
 * </p>
 *
 * @author plj
 * @since 2022-03-24
 */
@Controller
@RestController
@Api(description = "问题相关接口")
@RequestMapping("/question")
public class QuestionController {
    @Resource
    IQuestionService iQuestionService;

    @Resource
    IAttentionQuestionService iAttentionQuestionService;

    private final SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @ApiOperation(value = "发布问题")
    @PostMapping("addQuestion")
    public Result addQuestion(@RequestBody Question question){
        Date time = new Date();

        question.setId(UUID.randomUUID().toString());
        question.setCreateTime(sdf.format(time));

        if(iQuestionService.save(question)) return ResultGenerator.genSuccessResult(iQuestionService.getById(question.getId()));
        else return ResultGenerator.genFailResult("发布问题失败");
    }

    @ApiOperation(value = "删除问题")
    @PostMapping("delQuestion")
    public Result delQuestion(@RequestParam String id){
        if(iQuestionService.delById(id)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("问题删除失败");
    }

    @ApiOperation(value = "通过问题id获取问题详细信息")
    @GetMapping("getQuestionById")
    public Result getQuestionById(@RequestParam String id){
        Question result = iQuestionService.getById(id);
        if(result != null)
            return ResultGenerator.genSuccessResult(result);
        else
            return ResultGenerator.genFailResult("该问题不存在");
    }

    @ApiOperation(value = "编辑问题(只能更改标题和内容)")
    @PostMapping("editQuestion")
    public Result editQuestion(@RequestParam String questionId, @RequestParam String title, @RequestParam String content){
        if(iQuestionService.editQuestion(questionId, title, content)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("问题编辑失败");
    }

    @ApiOperation(value = "获取该用户所有问题， 返回分页结果")
    @GetMapping("getUserQuestions")
    public Result getAllQuestionByUserId(@RequestParam String userId, @RequestParam int pageNum, @RequestParam int pageSize){
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        return ResultGenerator.genSuccessResult(iQuestionService.getAllQuestionByUserId(userId, pageRequest));
    }

    @ApiOperation(value = "获取所有问题，结果根据浏览量排序, 返回分页结果")
    @GetMapping("getAllQuestion")
    public Result getAllQuestion(@RequestParam int pageNum, @RequestParam int pageSize){
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        PageResult results = iQuestionService.sortByWatchUserNum(pageRequest);
        return ResultGenerator.genSuccessResult(results);
    }

    @ApiOperation(value = "获取所有问题，结果顺序随机，返回分页结果")
    @GetMapping("getAllQuestionByRandom")
    public Result getAllQuestionByRandom(@RequestParam int pageNum, @RequestParam int pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        PageResult results = iQuestionService.sortRandom(pageRequest);
        return ResultGenerator.genSuccessResult(results);
    }

    @ApiOperation(value = "获取所有问题，结果根据时间排序，返回分页结果")
    @GetMapping("getAllQuestionByTime")
    public Result getAllQuestionByTime(@RequestParam int pageNum, @RequestParam int pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        PageResult results = iQuestionService.sortByTime(pageRequest);
        return ResultGenerator.genSuccessResult(results);
    }

    @ApiOperation(value = "问题增加浏览量")
    @PostMapping("addWatchNum")
    public Result addWatchNum(@RequestParam String questionId){
        if(iQuestionService.addWatchNum(questionId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("问题浏览量增加失败");
    }

    @ApiOperation(value = "问题增加赞同数")
    @PostMapping("addAgreeNum")
    public Result addAgreeNum(@RequestParam String questionId, @RequestParam String userId){
        if(iQuestionService.addAgreeNum(questionId, userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("增加失败");
    }

    @ApiOperation(value = "取消赞同")
    @PostMapping("reAgreeNum")
    public Result reAgreeNum(@RequestParam String questionId, @RequestParam String userId){
        if(iQuestionService.reAgreeNum(questionId, userId)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("取消赞同失败");
    }

    @ApiOperation(value = "是否赞同")
    @PostMapping("isAgree")
    public Result isAgree(@RequestParam String questionId, @RequestParam String userId){
        if(iQuestionService.agree(questionId, userId)) return ResultGenerator.genSuccessResult(true);
        else return ResultGenerator.genSuccessResult(false);
    }

    @ApiOperation(value = "关注问题")
    @PostMapping("addAttentionNum")
    public Result addAttentionNum(@RequestBody AttentionQuestion attentionQuestion){
        if(iQuestionService.addAttentionNum(attentionQuestion)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("问题关注数增加失败");
    }

    @ApiOperation(value = "是否关注问题")
    @PostMapping("isAttention")
    public Result isAttention(@RequestParam String questionId, @RequestParam String userId){
        if(iQuestionService.attention(questionId, userId)) return ResultGenerator.genSuccessResult(true);
        else return ResultGenerator.genSuccessResult(false);
    }

    @ApiOperation(value = "取消关注问题")
    @PostMapping("reAttentionNum")
    public Result reAttentionNum(@RequestBody AttentionQuestion attentionQuestion){
        if(iQuestionService.reAttentionNum(attentionQuestion)) return ResultGenerator.genSuccessResult();
        else return ResultGenerator.genFailResult("取消关注失败");
    }

    @ApiOperation(value = "获取用户关注的所有问题（分页）")
    @PostMapping("getAttentionQuestion")
    public Result getAttentionQuestion(@RequestParam String userId, @RequestParam int pageNum, @RequestParam int pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        List<String> questionIds = iAttentionQuestionService.getAllQuestionId(userId);

        return ResultGenerator.genSuccessResult(iQuestionService.getAttentionQuestion(questionIds,pageRequest));
    }

}
